package com.aaa.service.impl;

import com.aaa.dao.UserMapper;
import com.aaa.entity.User;
import com.aaa.service.UserService;
import com.aaa.util.JwtUtil;
import com.aaa.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 荆豪峰
 * @version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public Result delete(Integer id) {
        int i =  userMapper.deleteuser(id);
        return new Result(200,"删除成功",i);
    }

    @Override
    public Result add(User user) {
        int adduser = userMapper.adduser(user);
        return new Result(200,"添加成功",adduser);
    }

    @Override
    public Result update(User user) {
        int updateuser = userMapper.updateuser(user);

        return new Result(200,"修改成功",updateuser);
    }

    @Override
    public Result SelectAll(Integer currentPage, Integer pageSize) {
        //开启分页
        PageHelper.startPage(currentPage,pageSize);
        List<User> all = userMapper.selectAll();
        //处理数据
        PageInfo<User> pageInfo=new PageInfo<>(all);


        return new Result(200,"分页成功",pageInfo);
    }

    @Override
    public Result list() {
      List<User> list=  userMapper.list();
        return new Result(200,"成功",list);
    }

    @Override
    public Result findByNameAndPass(String username, String password) {
        String passwords = userMapper.selectByNameAndPass(username);
        if (passwords == null) {
            return new Result(5000,"账号错误");
        }if (passwords.equals(password)) {
            Map<String,Object> map =new HashMap<>();
            map.put("name",username);
            System.out.println(username);
            String token = JwtUtil.createToken(map);
            System.out.println(token);
            return new Result(2000,"登陆成功",token);
        }else {
            return new Result(3000,"密码错误");
        }
    }
}
